//
//  HotestSongsTableViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/17.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "HotestSongsTableViewCell.h"
#import "CustomerNetworking.h"
#import <UIImageView+WebCache.h>
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@interface HotestSongsTableViewCell ()

@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation HotestSongsTableViewCell
- (void)dealloc
{
    [_playImage release];
    [_songModel release];
    [_dataModel release];
    [_titleLabel release];
    [_subLabel release];
    [_songsPageModel release];
    [_songsArray release];
    [_numOneLabel release];
    [_numTwoLabel release];
    [_numThreeLabel release];
    [_oneSongLabel release];
    [_twoSongLabel release];
    [_threeSongLabel release];
    [_image release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        [self createSubViews];
        
        [self loadData];
        
        
    }
    return self;
}
-(void)loadData
{
    self.songsArray = [NSMutableArray array];
    self.array = [NSMutableArray array];
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        
        NSArray *listArray = [NSArray arrayWithArray:[[[[[response objectForKey:@"data"] objectAtIndex:9] objectForKey:@"data"] objectAtIndex:0] objectForKey:@"songs"]];
        for (NSDictionary *dic in listArray) {
            SongsModel *songModel = [SongsModel modelWithDictionary:dic];
            [self.songsArray addObject:songModel];
        }
        
        self.numOneLabel.text = @"1";
        self.numTwoLabel.text = @"2";
        self.numThreeLabel.text = @"3";
       
        self.oneSongLabel.text = [NSString stringWithFormat:@"%@ -%@", [_songsArray[0] name], [_songsArray[0] singerName]];
        
            
        self.twoSongLabel.text = [NSString stringWithFormat:@"%@ -%@", [_songsArray[1] name], [_songsArray[1] singerName]];
        
        self.threeSongLabel.text = [NSString stringWithFormat:@"%@ -%@", [_songsArray[2] name], [_songsArray[2] singerName]];


        
        
        NSArray *dataArray = [NSArray arrayWithArray:[[[response objectForKey:@"data"] objectAtIndex:9] objectForKey:@"data" ]];
        NSDictionary *dataDic = dataArray[0];
        DataModel *dataModel = [DataModel modelWithDictionary:dataDic];
        [self.array addObject:dataModel];
        [self.image sd_setImageWithURL:[NSURL URLWithString:dataModel.picUrl]];
        
        
        
        
        
        
    }];
}

-(void)createSubViews
{
    
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.subLabel = [[UILabel alloc] init];
    self.subLabel.textColor = [UIColor grayColor];
    self.subLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.subLabel];
    [_subLabel release];
    
    self.numOneLabel = [UILabel new];
    self.numOneLabel.font = [UIFont systemFontOfSize:15];
    self.numOneLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:self.numOneLabel];
    [_numOneLabel release];
    
    self.numTwoLabel = [UILabel new];
    self.numTwoLabel.font = [UIFont systemFontOfSize:15];
    self.numTwoLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:self.numTwoLabel];
    [_numTwoLabel release];
    
    self.numThreeLabel = [UILabel new];
    self.numThreeLabel.font = [UIFont systemFontOfSize:15];
    self.numThreeLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:self.numThreeLabel];
    [_numThreeLabel release];
    
    self.oneSongLabel = [UILabel new];
    self.oneSongLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.oneSongLabel];
    [_oneSongLabel release];
    
    self.twoSongLabel = [UILabel new];
    self.twoSongLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.twoSongLabel];
    [_twoSongLabel release];

    
    self.threeSongLabel = [UILabel new];
    self.threeSongLabel.font = [UIFont systemFontOfSize:15];
    [self.contentView addSubview:self.threeSongLabel];
    [_threeSongLabel release];
    
    self.image = [UIImageView new];
    [self.contentView addSubview:self.image];
    [_image release];
    
    
    self.playImage = [UIImageView new];
    [self.image addSubview:self.playImage];
    [_playImage release];

    
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGRect titleRect = [self.titleLabel.text boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.titleLabel.font,NSFontAttributeName, nil] context:nil];
    self.titleLabel.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), titleRect.size.width, HEIGHT_PRO(20));
    self.subLabel.frame = CGRectMake(self.titleLabel.frame.origin.x + self.titleLabel.frame.size.width + WIDTH_PRO(10), HEIGHT_PRO(10), WIDTH_PRO(100), HEIGHT_PRO(20));
    
    self.image.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(40), (SCREEN_WIDTH - WIDTH_PRO(40)) / 3, (SCREEN_WIDTH - WIDTH_PRO(40)) / 3);
    
    self.playImage.frame = CGRectMake(WIDTH_PRO(20), HEIGHT_PRO(20), self.image.frame.size.width - WIDTH_PRO(40), self.image.frame.size.height - HEIGHT_PRO(40));
    
    self.numOneLabel.frame = CGRectMake(self.image.frame.origin.x + self.image.frame.size.width + WIDTH_PRO(10), HEIGHT_PRO(50), WIDTH_PRO(10), HEIGHT_PRO(20));
    self.numTwoLabel.frame = CGRectMake(self.numOneLabel.frame.origin.x, self.numOneLabel.frame.origin.y + self.numOneLabel.frame.size.height + HEIGHT_PRO(10), WIDTH_PRO(10), HEIGHT_PRO(20));
    self.numThreeLabel.frame = CGRectMake(self.numTwoLabel.frame.origin.x, self.numTwoLabel.frame.origin.y + self.numTwoLabel.frame.size.height + HEIGHT_PRO(10), WIDTH_PRO(10), HEIGHT_PRO(20));
    
    
    self.oneSongLabel.frame = CGRectMake(self.numTwoLabel.frame.origin.x + self.numTwoLabel.frame.size.width + WIDTH_PRO(10), HEIGHT_PRO(50), WIDTH_PRO(200), HEIGHT_PRO(20));
    
   
    self.twoSongLabel.frame = CGRectMake(self.numTwoLabel.frame.origin.x + self.numTwoLabel.frame.size.width + WIDTH_PRO(10), self.oneSongLabel.frame.size.height + self.oneSongLabel.frame.origin.y + HEIGHT_PRO(10), WIDTH_PRO(200), HEIGHT_PRO(20));
    
        self.threeSongLabel.frame = CGRectMake(self.numTwoLabel.frame.origin.x + self.numTwoLabel.frame.size.width + WIDTH_PRO(10), self.twoSongLabel.frame.size.height + self.twoSongLabel.frame.origin.y + HEIGHT_PRO(10), WIDTH_PRO(200), HEIGHT_PRO(20));
}
-(void)setSongsPageModel:(SongsPageModel *)songsPageModel
{
    if (_songsPageModel != songsPageModel) {
        [_songsPageModel release];
        _songsPageModel = [songsPageModel retain];
    }
    
    self.titleLabel.text = songsPageModel.name;
    self.subLabel.text = songsPageModel.desc;
    self.playImage.image = [UIImage imageNamed:@"songlist_play_normal"];
}


+(HotestSongsTableViewCell *)hotestSongsTableViewCellWith:(UITableView *)tableView
{
    static NSString *cellIdentify = @"hotestSongs";
    HotestSongsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[HotestSongsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
